

import { wxLogin, articleCollect, Transpond} from "../../../api/eapi.js"

Page({
  data: {
    videoShowing: false,
    coverVideo: {
      video_path: '',
      poster: ''
    },
    windowx:'0',
    windowWidth:null,
    movabel: false,
    title:'My Drink',
    auto:true,
    nav:true,
    options:false,
    manage: false,
    articles: [],
    selectAll: false,
    ids: [] // 将要删除的
  },
  // 视频控制方面 S
  onReady: function (res) {
    // 获取视频播放器上下文
    this.videoContext = wx.createVideoContext('cv',this)
  },
  showCoverVideo(e) {
    if(this.data.movabel) {
      return ;
    }
    let {poster, video_path} = e.currentTarget.dataset;
    this.setData({videoShowing: true, coverVideo: {poster, video_path}}, ()=>this.videoContext.play())
  },
  closeCoverVideo(e) {
    let type = e.target.dataset.type;
    if(type !== 'video') {
      this.setData({videoShowing: false}, ()=>this.videoContext.pause())
    }
  },
  // 视频控制方面 E
  onShow: function () {
    articleCollect.get().then(resp=>{
      console.log(resp)
      let articles = resp.data.map((item)=>item.article);
      // 添加已选择标记
      articles.forEach(item => item.select = false);
      this.setData({articles: articles},()=>console.log(this.data.articles))
    }).catch(err => {
      if(err.code === 401) {
        console.log(401)
        wxLogin();
        wx.reLaunch({url: getCurrentPages()[getCurrentPages().length-1].route});
      }
    })
  },
  changeManage(){
    this.setData({
      manage: !this.data.manage,
      windowx:'0',
      options:!this.data.options,
      auto:!this.data.auto,
      movabel:!this.data.movabel
    })
  },
  manage(){
    this.setData({
      windowx:'0',
      options:!this.data.options,
      auto:!this.data.auto,
      movabel:!this.data.movabel
    }, ()=>console.log(this.data.movabel))
  },
  del(e) {
    let target = e.target.dataset;
    let ids = "";
    console.log(target)
    if(target.type !== 'multi') {
      ids += target.id;
    }else {
      // 获取所有被选择的收藏id
      console.log(this.data.articles)
      ids = this.data.articles.filter(item => item.select).map(item => item.id).join(',')
    }
    articleCollect.delete({ids: ids}).then(resp => {
      console.log(resp)
      wx.showToast({title: '删除中'})
      articleCollect.get().then(resp=>{
        console.log(resp)
        let articles = resp.data.map((item)=>item.article);
        articles.forEach(item => item.select = false);
        this.setData({articles: articles},()=>wx.showToast({title: '完成'}))
      })
    });
    this.setData({
      windowx:'0'
    })
    wx.setStorage({
      key: 'homeData',
      data: true,
    })
    wx.setStorage({
      key: 'me_collection_change',
      data: `true`
    })
  },
  select(e) {
    console.log(e)
    let target = e.currentTarget.dataset;
    let articles = this.data.articles;
    let isSelectAll = true;

    if(target.type === 'selectAll') {

      if(!this.data.selectAll) {
        articles.forEach(item => item.select = true);
        this.setData({articles: articles, selectAll: true});
        return ;
      } else {
        articles.forEach(item => item.select = false);
        this.setData({articles: articles, selectAll: false});
        return ;
      }

    }

    articles.forEach(item => {
      if(item.id === target.id) {
        item.select = !item.select;
      }
    })

    articles.forEach(item => {
      if(!item.select) {
        isSelectAll = false;
      }
    })
    this.setData({articles: articles, selectAll: isSelectAll});
  },
  onShareAppMessage:function(res){
    return Transpond(res)
  }
})
